﻿namespace WMS6.BLL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.DALFactory;

    public class WaveFlowDetailBLL
    {
        private static readonly ICommonMethodDA commonMethod = DataAccess.CreateCommonMethod();
        private List<string> dataBaseID;
        private PubParasInfo pubParasInfo;
        private static readonly IWaveFlowDA waveFlowDA = DataAccess.CreateWaveFlowDA();
        private static readonly IWaveFlowDetailDA waveFlowDetailDA = DataAccess.CreateWaveFlowDetailDA();

        public WaveFlowDetailBLL()
        {
            this.pubParasInfo = null;
        }

        public WaveFlowDetailBLL(string currentWH)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(currentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(currentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001");
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002");
            }
        }

        public WaveFlowDetailBLL(PubParasInfo tempPubParasInfo)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(tempPubParasInfo.CurrentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(tempPubParasInfo.CurrentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001", this.pubParasInfo.UserLanguage);
                }
                else
                {
                    this.pubParasInfo = new PubParasInfo();
                    this.pubParasInfo = tempPubParasInfo;
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002", this.pubParasInfo.UserLanguage);
            }
        }

        public string DeleteWaveFlowInfo(List<string> waveFlowID, List<string> lineID)
        {
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            string temp = string.Empty;
            try
            {
                int i;
                List<WaveFlowDetailInfo> waveFlowDetailInfoList = new List<WaveFlowDetailInfo>();
                WaveFlowDetailInfo waveFlowDetailInfo = null;
                AuditDataBLL auditDataBLL = new AuditDataBLL();
                for (i = 0; i < waveFlowID.Count; i++)
                {
                    waveFlowDetailInfo = waveFlowDetailDA.GetWaveFlowDetailByIDLineID(dataBase, tran, waveFlowID[i], lineID[i], this.dataBaseID[0], this.pubParasInfo);
                    waveFlowDetailInfoList.Add(waveFlowDetailInfo);
                }
                if (waveFlowDetailDA.DeleteWaveFlowDetailByID(dataBase, tran, waveFlowID, lineID, this.dataBaseID[0]) > 0)
                {
                    for (i = 0; i < waveFlowID.Count; i++)
                    {
                        auditDataBLL.InsertAuditData<WaveFlowDetailInfo>(dataBase, tran, waveFlowDetailInfoList[i], null, AuditAction.Delete, this.pubParasInfo.UserID, "WAVE_FLOW_DETAIL", this.dataBaseID[0]);
                        string CS00002 = temp;
                        temp = CS00002 + waveFlowID[i] + "/" + lineID[i] + ",";
                    }
                    temp = temp.Substring(0, temp.Length - 1);
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return temp;
        }

        public DataSet GetAllWaveFlowDetailInfo()
        {
            return waveFlowDetailDA.GetAllWaveFlowDetail(this.dataBaseID[0]);
        }

        public List<WaveFlowDetailInfo> GetWaveFlowDetailInfoByID(string waveFlowID)
        {
            List<WaveFlowDetailInfo> waveFlowDetailInfoList = new List<WaveFlowDetailInfo>();
            try
            {
                waveFlowDetailInfoList = waveFlowDetailDA.GetWaveFlowDetailListByID(waveFlowID, this.dataBaseID[0], this.pubParasInfo);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            return waveFlowDetailInfoList;
        }

        public WaveFlowDetailInfo GetWaveFlowDetailInfoByIDLineID(string waveFlowID, string lineID)
        {
            WaveFlowDetailInfo waveFlowDetailInfo = null;
            try
            {
                waveFlowDetailInfo = waveFlowDetailDA.GetWaveFlowDetailByIDLineID(waveFlowID, lineID, this.dataBaseID[0], this.pubParasInfo);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            return waveFlowDetailInfo;
        }

        public string InsertWaveFlowDetailInfo(WaveFlowDetailInfo waveFlowDetailInfo)
        {
            string whLoginID = this.dataBaseID[0];
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            string strWaveFlowID = string.Empty;
            try
            {
                waveFlowDetailInfo.CreatedBy = this.pubParasInfo.UserID;
                waveFlowDetailInfo.UpdatedBy = waveFlowDetailInfo.CreatedBy;
                if (waveFlowDetailDA.InsertWaveFlowDetail(dataBase, tran, waveFlowDetailInfo, whLoginID) > 0)
                {
                    strWaveFlowID = waveFlowDetailInfo.WaveFlowID + "/" + waveFlowDetailInfo.LineID;
                    new AuditDataBLL().InsertAuditData<WaveFlowDetailInfo>(dataBase, tran, null, waveFlowDetailInfo, AuditAction.Insert, this.pubParasInfo.UserID, "WAVE_FLOW_DETAIL", this.dataBaseID[0]);
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return strWaveFlowID;
        }

        public string UpdateWaveFlowDetailInfo(WaveFlowDetailInfo waveFlowDetailInfo)
        {
            string whLoginID = this.dataBaseID[0];
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            string strWaveFlowID = string.Empty;
            try
            {
                WaveFlowDetailInfo oldWaveFlowDetailInfo = waveFlowDetailDA.GetWaveFlowDetailByIDLineID(dataBase, tran, waveFlowDetailInfo.WaveFlowID, waveFlowDetailInfo.LineID.ToString(), whLoginID, this.pubParasInfo);
                if (waveFlowDetailDA.UpdateWaveFlowDetail(dataBase, tran, waveFlowDetailInfo, whLoginID) > 0)
                {
                    strWaveFlowID = waveFlowDetailInfo.WaveFlowID + "/" + waveFlowDetailInfo.LineID;
                    new AuditDataBLL().InsertAuditData<WaveFlowDetailInfo>(dataBase, tran, oldWaveFlowDetailInfo, waveFlowDetailInfo, AuditAction.Update, this.pubParasInfo.UserID, "WAVE_FLOW_DETAIL", this.dataBaseID[0]);
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return strWaveFlowID;
        }
    }
}

